package edu.rit.teamrocket.view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class Pie extends JComponent
{
	private static final long serialVersionUID = -4071747709809161595L;
	ArrayList<Slice> slices = new ArrayList<Slice>();
	Color[] colors = {Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY,
			Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA,
			Color.ORANGE, Color.PINK, Color.RED, Color.YELLOW};

	public Pie(ArrayList<Double> array)
	{
		for (int i = 0; i < array.size(); i++)
			slices.add(new Slice(array.get(i), colors[i]));
	}

	@Override
	public void paint(Graphics g)
	{
		if (!slices.isEmpty())
			drawPie((Graphics2D) g);
	}

	void drawPie(Graphics2D g)
	{
		double total = 0D;
		Rectangle area = getBounds();
		for (int i = 0; i < slices.size(); i++)
		{
			total += slices.get(i).value;
		}
		double curValue = 0.0D;
		int startAngle = 0;
		for (int i = 0; i < slices.size(); i++)
		{
			startAngle = (int) (curValue * 360 / total);
			int arcAngle = (int) (slices.get(i).value * 360 / total);
			g.setColor(slices.get(i).color);
			g.fillArc(area.x, area.y, area.width, area.height, startAngle, arcAngle);
			curValue += slices.get(i).value;
		}
	}

	public static void main(String[] t)
	{
		JFrame frame = new JFrame("Pie chart");
		ArrayList<Double> array = new ArrayList<Double>();
		if (t.length > 0)
			for (String s : t)
				array.add(Double.parseDouble(s));
		frame.add(new Pie(array));
		frame.setVisible(true);
		frame.setSize(500, 500);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}